import axios, { AxiosResponse } from "axios";
import { userInfoStore } from "../userStore/userStore";

// 管理员API类
class AdminApis {
    // 获取当前管理员ID
    private getAdminId(): string {
        const userStore = userInfoStore();
        return userStore.id;
    }

    // 获取仪表板数据
    getDashboard(): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.get('/api/admin/dashboard', {
                params: { admin_id: this.getAdminId() }
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 获取用户列表
    getUsers(page: number = 1, pageSize: number = 10, keyword: string = ''): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.get('/api/admin/users', {
                params: { admin_id: this.getAdminId(), page, page_size: pageSize, keyword }
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 更新用户余额
    updateUserBalance(userId: number | string, amount: number): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/user/balance', {
                admin_id: this.getAdminId(),
                user_id: userId,
                amount: amount
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 获取商家列表
    getStores(page: number = 1, pageSize: number = 10, keyword: string = '', status: string = ''): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.get('/api/admin/stores', {
                params: { admin_id: this.getAdminId(), page, page_size: pageSize, keyword, status }
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 审核商家
    reviewStore(storeId: number, status: number, reason: string = ''): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/stores/review', {
                admin_id: this.getAdminId(),
                store_id: storeId,
                status: status,
                reason: reason
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 获取待审核商家列表
    getPendingStores(): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.get('/api/admin/stores/pending', {
                params: { admin_id: this.getAdminId() }
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 获取外卖列表
    getTakeouts(page: number = 1, pageSize: number = 10, keyword: string = '', status: string = ''): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.get('/api/admin/takeouts', {
                params: { admin_id: this.getAdminId(), page, page_size: pageSize, keyword, status }
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 审核外卖
    reviewTakeout(takeoutId: number, status: number, reason: string = ''): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/takeouts/review', {
                admin_id: this.getAdminId(),
                takeout_id: takeoutId,
                status: status,
                reason: reason
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 获取待审核外卖列表
    getPendingTakeouts(): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.get('/api/admin/takeouts/pending', {
                params: { admin_id: this.getAdminId() }
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 获取订单列表
    getOrders(page: number = 1, pageSize: number = 10, keyword: string = '', status: string = ''): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.get('/api/admin/orders', {
                params: { admin_id: this.getAdminId(), page, page_size: pageSize, keyword, status }
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 获取统计数据
    getStatistics(startDate: string = '', endDate: string = ''): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.get('/api/admin/statistics', {
                params: { admin_id: this.getAdminId(), start_date: startDate, end_date: endDate }
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // ==================== 用户增删改功能 ====================

    // 删除用户
    deleteUser(userId: string): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/user/delete', {
                admin_id: this.getAdminId(),
                user_id: userId
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 编辑用户信息
    updateUser(userId: string, username: string, phone: string): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/user/update', {
                admin_id: this.getAdminId(),
                user_id: userId,
                username: username,
                phone: phone
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 新增用户
    createUser(username: string, phone: string, password: string, isAdmin: boolean = false, balance?: number): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/user/create', {
                admin_id: this.getAdminId(),
                username: username,
                phone: phone,
                password: password,
                is_admin: isAdmin,
                balance: balance
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // ==================== 商家删除功能 ====================

    // 删除商家
    deleteStore(storeId: number): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/store/delete', {
                admin_id: this.getAdminId(),
                store_id: storeId
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // ==================== 外卖删除功能 ====================

    // 删除外卖
    deleteTakeout(takeoutId: number): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/takeout/delete', {
                admin_id: this.getAdminId(),
                takeout_id: takeoutId
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // ==================== 订单管理功能 ====================

    // 删除订单
    deleteOrder(orderId: string): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/order/delete', {
                admin_id: this.getAdminId(),
                order_id: orderId
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    // 修改订单状态
    updateOrderState(orderId: string, state: number): Promise<any> {
        const promise: Promise<any> = new Promise((resolve, reject) => {
            axios.post('/api/admin/order/update-state', {
                admin_id: this.getAdminId(),
                order_id: orderId,
                state: state
            })
                .then((result: AxiosResponse) => {
                    if (result.data.code === 200) {
                        resolve(result.data.data);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

// 导出单例
export const adminApis = new AdminApis();
